######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Word Automation Service
# -----------------------------------------------------------------
function New-WordAutomationService()
{
    $obj=new-object PSObject -Property @{
      Name = "Word Automation Service";
      Proxy = "Word Automation Service Proxy";
      Pool = New-ApplicationPool;
      DB = New-Database;
      Service = New-SupportingService;
      Default = $true;
      ActiveProcesses = 8;
      ConversionsPerInstance = 12;
      ConversionTimeout = 5;
      DisableBinaryFileScan = $false;
      DisableEmbeddedFonts = $false;
      KeepAliveTimeout = 30;
      MaximumConversionAttempts = 2;
      MaximumMemoryUsage = 100;
      RecycleProcessThreshold = 100;
      TimerJobFrequency = 15;
    }
    $obj.Service.Name = "Word Automation Services"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Word Automation Service"
        $this.Pool.Validate()
        $this.DB.Validate()
        $this.Service.Validate()
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy 
        Validate-Range "ActiveProcesses" $this.ActiveProcesses 1 1000
        Validate-Range "ConversionTimeout" $this.ConversionTimeout 1 60
        Validate-Range "KeepAliveTimeout" $this.KeepAliveTimeout 1 60
        Validate-Range "MaximumConversionAttempts" $this.MaximumConversionAttempts 1 60
        Validate-Range "MaximumMemoryUsage" $this.MaximumMemoryUsage 10 100
        Validate-Range "RecycleProcessThreshold" $this.RecycleProcessThreshold 10 1000
        Validate-Range "TimerJobFrequency" $this.TimerJobFrequency 1 59
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            $this.DB.Provision()
            $this.DB.AddRole($farm.DeploymentAccount, "db_owner")
            $this.DB.AddRole($this.Pool.Account, "db_owner")
                        
            $sa = New-SPWordConversionServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -Default:$this.Default `
                    -DatabaseServer $this.DB.Server `
                    -DatabaseName $this.DB.FullName
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
         
        Set-SPWordConversionServiceApplication -Identity $sa `
             -ActiveProcesses $this.ActiveProcesses `
             -ConversionsPerInstance $this.ConversionsPerInstance `
             -ConversionTimeout $this.ConversionTimeout `
             -DisableBinaryFileScan:$this.DisableBinaryFileScan `
             -DisableEmbeddedFonts:$this.DisableEmbeddedFonts `
             -KeepAliveTimeout $this.KeepAliveTimeout `
             -MaximumConversionAttempts $this.MaximumConversionAttempts `
             -MaximumMemoryUsage $this.MaximumMemoryUsage `
             -RecycleProcessThreshold $this.RecycleProcessThreshold `
             -TimerJobFrequency $this.TimerJobFrequency `
        
        #proxy automatically created
        
        $this.Service.Provision()
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
